package com.gitee.zycra.future.job.dao;

import com.gitee.zycra.future.job.common.bo.PageQuery;
import com.gitee.zycra.future.job.common.model.JobMetaData;
import com.gitee.zycra.future.job.common.param.PageQueryMetaDataParam;
import com.gitee.zycra.future.job.common.vo.JobMetaDataVO;
import com.gitee.zycra.jdbc.common.BaseDao;
import com.gitee.zycra.jdbc.common.CommonSelector;
import com.gitee.zycra.jdbc.enums.SQLConditionEnum;
import com.gitee.zycra.jdbc.enums.SQLLinkEnum;
import com.gitee.zycra.jdbc.model.PageQueryWrapper;
import com.gitee.zycra.jdbc.model.PageResult;
import com.gitee.zycra.jdbc.util.SQLBlock;
import com.gitee.zycra.jdbc.util.SQLChain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * @author zycra
 */
@Repository
public class JobMetaDataDao extends BaseDao<JobMetaData> {

    @Autowired
    private CommonSelector commonSelector;

    /**
     * 插入
     *
     * @param jobMetaData 元数据对象
     * @return 插入条数
     */
    public int insertJobMetaData(JobMetaData jobMetaData) {
        return insertIntoTable(jobMetaData);
    }

    /**
     * 更新
     *
     * @param jobMetaData 元数据对象
     * @return 更新条数
     */
    public int updateJobMetaData(JobMetaData jobMetaData) {
        return updateTableById(jobMetaData);
    }

    /**
     * 分页查询
     *
     * @param pageQuery 查询参数
     * @return 查询到的数据
     */
    public PageResult<JobMetaDataVO> selectJobMetaDataForPage(PageQuery<PageQueryMetaDataParam> pageQuery) {
        PageQueryMetaDataParam queryParam = pageQuery.getParam();
        SQLChain sqlChain = SQLChain.builder()
                .addBlock(SQLBlock.of(SQLLinkEnum.AND, "deleted", SQLConditionEnum.EQUALS, 0))
                .addBlock(SQLBlock.of(SQLLinkEnum.AND, "application_name", SQLConditionEnum.CONTAINS, queryParam.getApplicationName()))
                .addBlock(SQLBlock.of(SQLLinkEnum.AND, "job_class_name", SQLConditionEnum.CONTAINS, queryParam.getJobClassName()))
                .addBlock(SQLBlock.of(SQLLinkEnum.AND, "job_name", SQLConditionEnum.CONTAINS, queryParam.getJobName()))
                .build();
        String sql = "SELECT jmd.id, jmd.application_name, jmd.job_class_name, jmd.job_name, jmd.job_retry_times, jmd.job_retry_interval, "
                + "jmd.job_cycle, jmd.operator_name, jmd.update_time, (SELECT COUNT(*) > 0 FROM job_schedule js WHERE js.job_meta_data_id = jmd.id "
                + "AND js.deleted = 0) AS 'hasSchedule' FROM job_meta_data jmd";
        PageQueryWrapper param = new PageQueryWrapper(getCountSqlWithOutParam() + sqlChain.getSQL(),
                sqlChain.getParamList(), sql + sqlChain.getSQL() + " ORDER BY id DESC", sqlChain.getParamList(),
                pageQuery.getPage(),
                pageQuery.getSize());
        return commonSelector.selectForPage(getJdbcTemplate(), JobMetaDataVO.class, param);
    }

    /**
     * 根据元数据配置ID查询
     *
     * @param jobMetaDataId 元数据配置ID
     * @return 元数据
     */
    public JobMetaData selectById(Long jobMetaDataId) {
        return selectTableById(new JobMetaData().setId(jobMetaDataId));
    }
}
